/**
 * Created by root on 17-7-27.
 */
var schedule = require('node-schedule');
var http = require('../utils/http');
var logger = require('../config/log4js');
var cheerio = require('cheerio');
var ipListService = require('../service/ipListService');

var jobs = [];

/**
 * 西刺 国内高匿代理
 */
function xiciNNJion() {
    var rule = new schedule.RecurrenceRule();
    rule.minute = [0, 15, 30, 45];
    var job = schedule.scheduleJob(rule, function () {
        logger.access.info('xiciHttpProxyTask定时任务: ' + new Date());
        http.httpGetByURL("http://www.xicidaili.com/nn/", function (result) {
            if (result.statusCode === 200) {
                var $ = cheerio.load(result.body);
                $('#ip_list').find('tr').each(function () {
                    var ipList = $(this).children();
                    if ($(ipList[1]).text() !== 'IP地址') {
                        logger.access.info($(ipList[1]).text() + ":" + $(ipList[2]).text() + " " + $(ipList[5]).text());
                        if ($(ipList[5]).text() === 'HTTP') {
                            ipListService.addIP({
                                ip: $(ipList[1]).text(),
                                port: $(ipList[2]).text(),
                                type: 0
                            }, function (result) {
                            });
                        }
                        if ($(ipList[5]).text() === 'HTTPS') {
                            ipListService.addIP({
                                ip: $(ipList[1]).text(),
                                port: $(ipList[2]).text(),
                                type: 1
                            }, function (result) {
                            });
                        }
                    }
                });
            } else {
                logger.error.info(result);
            }
        });
    });
    jobs.push(job);
}


/**
 * 西刺 国内透明代理
 */
function xiciNTJion() {
    var rule = new schedule.RecurrenceRule();
    rule.minute = [0, 15, 30, 45];
    var job = schedule.scheduleJob(rule, function () {
        logger.access.info('xiciHttpProxyTask定时任务: ' + new Date());
        http.httpGetByURL("http://www.xicidaili.com/nt/", function (result) {
            if (result.statusCode === 200) {
                var $ = cheerio.load(result.body);
                $('#ip_list').find('tr').each(function () {
                    var ipList = $(this).children();
                    if ($(ipList[1]).text() !== 'IP地址') {
                        logger.access.info($(ipList[1]).text() + ":" + $(ipList[2]).text() + " " + $(ipList[5]).text());
                        if ($(ipList[5]).text() === 'HTTP') {
                            ipListService.addIP({
                                ip: $(ipList[1]).text(),
                                port: $(ipList[2]).text(),
                                type: 0
                            }, function (result) {
                            });
                        }
                        if ($(ipList[5]).text() === 'HTTPS') {
                            ipListService.addIP({
                                ip: $(ipList[1]).text(),
                                port: $(ipList[2]).text(),
                                type: 1
                            }, function (result) {
                            });
                        }
                    }
                });
            } else {
                logger.error.info(result);
            }
        });
    });
    jobs.push(job);
}

/**
 * 西刺 HTTPS代理
 */
function xiciWNJion() {
    var rule = new schedule.RecurrenceRule();
    rule.minute = [0, 15, 30, 45];
    var job = schedule.scheduleJob(rule, function () {
        logger.access.info('xiciHttpProxyTask定时任务: ' + new Date());
        http.httpGetByURL("http://www.xicidaili.com/wn/", function (result) {
            if (result.statusCode === 200) {
                var $ = cheerio.load(result.body);
                $('#ip_list').find('tr').each(function () {
                    var ipList = $(this).children();
                    if ($(ipList[1]).text() !== 'IP地址') {
                        logger.access.info($(ipList[1]).text() + ":" + $(ipList[2]).text() + " " + $(ipList[5]).text());
                        if ($(ipList[5]).text() === 'HTTP') {
                            ipListService.addIP({
                                ip: $(ipList[1]).text(),
                                port: $(ipList[2]).text(),
                                type: 0
                            }, function (result) {
                            });
                        }
                        if ($(ipList[5]).text() === 'HTTPS') {
                            ipListService.addIP({
                                ip: $(ipList[1]).text(),
                                port: $(ipList[2]).text(),
                                type: 1
                            }, function (result) {
                            });
                        }
                    }
                });
            } else {
                logger.error.info(result);
            }
        });
    });
    jobs.push(job);
}


/**
 * 西刺 HTTP代理
 */
function xiciWTJion() {
    var rule = new schedule.RecurrenceRule();
    rule.minute = [0, 15, 30, 45];
    var job = schedule.scheduleJob(rule, function () {
        logger.access.info('xiciHttpProxyTask定时任务: ' + new Date());
        http.httpGetByURL("http://www.xicidaili.com/wt/", function (result) {
            if (result.statusCode === 200) {
                var $ = cheerio.load(result.body);
                $('#ip_list').find('tr').each(function () {
                    var ipList = $(this).children();
                    if ($(ipList[1]).text() !== 'IP地址') {
                        logger.access.info($(ipList[1]).text() + ":" + $(ipList[2]).text() + " " + $(ipList[5]).text());
                        if ($(ipList[5]).text() === 'HTTP') {
                            ipListService.addIP({
                                ip: $(ipList[1]).text(),
                                port: $(ipList[2]).text(),
                                type: 0
                            }, function (result) {
                            });
                        }
                        if ($(ipList[5]).text() === 'HTTPS') {
                            ipListService.addIP({
                                ip: $(ipList[1]).text(),
                                port: $(ipList[2]).text(),
                                type: 1
                            }, function (result) {
                            });
                        }
                    }
                });
            } else {
                logger.error.info(result);
            }
        });
    });
    jobs.push(job);
}

module.exports = {
    start : function () {
        xiciNNJion();
        xiciNTJion();
        xiciWNJion();
        xiciWTJion();

        logger.access.info('xiciHttpProxyTask Start!');
    },
    stop : function () {
        for (var i = 0; i < jobs.length; i++) {
            jobs[i].cancel();
        }
    }
}
